#!/usr/bin/perl
#
# Hal interface
# GNU General Public License

package HalInterface;

use Net::DBus;

sub new
{
    my $self = {};

    my $dbus = Net::DBus->find;
    die("dbus error: $!\n") unless($dbus);

    my $hal = $dbus->get_service('org.freedesktop.Hal');
    die("hal error: $!\n") unless($hal);

    $self->{HAL} = $hal;
    $self->{HAL_MANAGER} = $hal->get_object('/org/freedesktop/Hal/Manager');


    bless $self;
    return $self;
}

sub get_object
{
    my $self = shift;
    my $udi = shift;

    return $self->{HAL}->get_object($udi);
}

sub get_manager
{
    my $self = shift;
    return $self->{HAL_MANAGER};
}

sub find_by_capability
{
    my $self = shift;
    my $cap = shift;

    my @udis = map($self->{HAL}->get_object($_)->GetProperty('info.udi'),
            @{ $self->{HAL_MANAGER}->FindDeviceByCapability($cap) });

    return \@udis;
}

sub find_by_capability_and_property
{
    my $self = shift;
    my $cap = shift;
    my $key = shift;
    my $val = shift;

    foreach $udi (@{ $self->find_by_capability($cap) })
    {
	return $udi if($self->get_property($udi, $key) eq $val);
    }

    return "";
}

sub find_by_property
{
    my $self = shift;
    my $key = shift;
    my $val = shift;

    my @udis = map($self->{HAL}->get_object($_)->GetProperty('info.udi'),
            @{ $self->{HAL_MANAGER}->FindDeviceStringMatch($key, $val) });

    return \@udis;
}

sub get_property
{
    my $self = shift;
    my $udi = shift;
    my $key = shift;

    my $obj = $self->{HAL}->get_object($udi);

    return $obj && $obj->PropertyExists($key) ? $obj->GetProperty($key) : "";
}

return 1;
